/*
 * Copyright (c) 2007 Compl Yue Still, All Rights Reserved.
 */

package oex.tasks;

import hbi.domain.Gateway;
import hbi.task.TaskAgent;
import hbi.task.TaskScript;
import oex.ObjectExplorer;
import oex.ObjectWorld;
import oex.data.ObjectShape;

public class SubmitShape implements TaskAgent<ObjectWorld, ObjectExplorer>
{

    private transient Gateway<? extends ObjectWorld, ? extends ObjectExplorer> gateway;

    public void arrived(
        Gateway<? extends ObjectWorld, ? extends ObjectExplorer> gateway)
    {
        this.gateway = gateway;
    }

    private transient ObjectShape shape = null;

    public void withNewShape(String type, final int tempOID)
    {
        shape = gateway.getLocalDomain().newShape(type);
        gateway.sendTaskAgent(ShapeApproval.class,
            new TaskScript<ShapeApproval>()
            {
                public void run(ShapeApproval agent)
                {
                    agent.newShapeApproved(tempOID, shape.oid);
                }
            });
    }

    public void withShape(int oid)
    {
        shape = gateway.getLocalDomain().getShape(oid);
    }

    public void setShapeProperty(String name, Object value)
    {
        shape.setProperty(name, value);
    }

    public void publishShape()
    {
        gateway.sendTaskAgent(ShapeApproval.class,
            new TaskScript<ShapeApproval>()
            {
                public void run(ShapeApproval agent)
                {
                    agent.shapeUpdateApproved(shape.oid);
                }
            });
        gateway.getLocalDomain().publishShape2All(shape);
    }

    public void removeShape(final int oid)
    {
        ObjectWorld world = gateway.getLocalDomain();
        if (world.removeShape(oid))
        {
            gateway.sendTaskAgent(ShapeApproval.class,
                new TaskScript<ShapeApproval>()
                {
                    public void run(ShapeApproval agent)
                    {
                        agent.shapeRemovalApproved(oid);
                    }
                });
            world.publishShapeRemoval2All(oid);
        }
    }

    public void finishTask(
        Gateway<? extends ObjectWorld, ? extends ObjectExplorer> gateway)
    {
    }

}
